import * as types from '../mutation-types'
import DEVICE_TYPES from 'config/device-types'
import _ from 'lodash'
import api from 'api'
import PRODUCTIDS from 'config/product-ids'
import WATERDATA from 'static/data/devices/water.json'
import store from 'src/store' // vuex store instance

// 定义变量,等所有请求都返回之后再丢到getters中
let waterNumber = 0
let waterArr = []

// initial state
const state = {
  devices: []
}

// actions
const actions = {
  updateWaterDeviceStatus ({ commit }, _self) {
    let tempHumidity = []
    let floodingId = []
    let levelId = []
    WATERDATA.forEach(element => {
      let tem
      tem = _.find(element.devices, (item) => {
        return item.type === DEVICE_TYPES.TEMP_HUMIDITY
      })
      tem['title'] = element.title
      tem['area'] = element.area
      tempHumidity.push(tem)
      let flo
      flo = _.find(element.devices, (item) => {
        return item.type === DEVICE_TYPES.FLOODING
      })
      flo['title'] = element.title
      flo['area'] = element.area
      floodingId.push(flo)
      let level
      level = _.filter(element.devices, (item) => {
        return item.type === DEVICE_TYPES.LEVEL
      })
      level['title'] = element.title
      level['area'] = element.area
      levelId.push(level)
    })
    // 请求水浸状态数据
    floodingId.forEach(item => {
      api.device.getVDevice(PRODUCTIDS[DEVICE_TYPES.FLOODING], item.id).then(function (doc) {
        if (typeof doc.data[25] === 'number') {
          let obj = {
            id: item.id,
            type: DEVICE_TYPES.FLOODING,
            isWarnning: false, // 是否告警
            data: {
              '25': doc.data[25]
            },
            room: item.title, // 所属位置
            area: item.area // 所属项目
          }
          commit(types.UPDATE_WATER_DEVICE_STATUS, obj)
        }
      }, function (error) {
        if (error.response.status !== 200) {
          _self.handleError(error)
        }
      })
    })
    // 请求温度湿度数据
    tempHumidity.forEach(element => {
      api.device.getVDevice(PRODUCTIDS[DEVICE_TYPES.TEMP_HUMIDITY], element.id).then(function (doc) {
        if (typeof doc.data[13] === 'number' && typeof doc.data[14] === 'number') {
          let obj = {
            id: element.id,
            type: DEVICE_TYPES.TEMP_HUMIDITY,
            isWarnning: false, // 是否告警
            data: {
              '13': doc.data[13],
              '14': doc.data[14]
            },
            room: element.title, // 所属位置
            area: element.area // 所属项目
          }
          commit(types.UPDATE_WATER_DEVICE_STATUS, obj)
        }
      }, function (error) {
        if (error.response.status !== 200) {
          _self.handleError(error)
        }
      })
    })
    levelId.forEach(element => {
      // 请求液位数据
      for (let i = 0; i < element.length; i++) {
        api.device.getVDevice(PRODUCTIDS[DEVICE_TYPES.LEVEL], element[i].id).then(function (doc) {
          if (typeof doc.data[26] === 'number') {
            let obj = {
              id: element[i].id,
              type: DEVICE_TYPES.LEVEL,
              isWarnning: false, // 是否告警
              data: {
                '26': doc.data[26]
              },
              room: element.title, // 所属位置
              area: element.area // 所属项目
            }
            commit(types.UPDATE_WATER_DEVICE_STATUS, obj)
          }
        }, function (error) {
          if (error.response.status !== 200) {
            _self.handleError(error)
          }
        })
      }
    })
  },
  // 水管压数据（管压数值与是否在线是否告警）部分
  updateWaterStatistics ({ commit }, _self) {
    let tubs = []
    WATERDATA.forEach(element => {
      let res = []
      res = _.filter(element.devices, (item) => {
        return item.type === DEVICE_TYPES.TUBE_PRESSURE
      })
      res['title'] = element.title
      res['area'] = element.area
      tubs.push(res)
    })
    tubs.forEach(element => {
      element.forEach(item => {
        api.device.getVDevice(PRODUCTIDS[DEVICE_TYPES.TUBE_PRESSURE], item.id).then(function (doc) {
          waterNumber++
          let obj = {
            id: item.id,
            type: DEVICE_TYPES.TUBE_PRESSURE,
            isWarnning: false, // 是否告警
            online: doc.data.online,
            data: {
              '27': doc.data[27]
            },
            room: element.title, // 所属位置
            area: element.area // 所属项目
          }
          commit(types.UPDATE_WATER_STATISTICS, obj)
        }, function (error) {
          waterNumber++
          _self.handleError(error)
        })
      })
    })
  }
}

// getters
const getters = {
  tubes () {
    switch (store.state.route.path.split('/')[1]) {
      case ':0':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TUBE_PRESSURE && item.area === '天悦'
        })
      case ':1':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TUBE_PRESSURE && item.area === '香槟'
        })
      case ':2':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TUBE_PRESSURE && item.area === '香槟'
        })
      case ':3':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TUBE_PRESSURE && item.area === '香槟'
        })
    }
  },
  // 用于首页做统计
  tubesHome () {
    return _.filter(state.devices, (item) => {
      return item.type === DEVICE_TYPES.TUBE_PRESSURE
    })
  },
  waterTempHumidity () {
    switch (store.state.route.path.split('/')[1]) {
      case ':0':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TEMP_HUMIDITY && item.area === '天悦'
        })
      case ':1':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TEMP_HUMIDITY && item.area === '香槟'
        })
      case ':2':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TEMP_HUMIDITY && item.area === '香槟'
        })
      case ':3':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.TEMP_HUMIDITY && item.area === '香槟'
        })
    }
  },
  flooding () {
    switch (store.state.route.path.split('/')[1]) {
      case ':0':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.FLOODING && item.area === '天悦'
        })
      case ':1':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.FLOODING && item.area === '香槟'
        })
      case ':2':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.FLOODING && item.area === '香槟'
        })
      case ':3':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.FLOODING && item.area === '香槟'
        })
    }
  },
  level () {
    switch (store.state.route.path.split('/')[1]) {
      case ':0':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.LEVEL && item.area === '天悦'
        })
      case ':1':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.LEVEL && item.area === '香槟'
        })
      case ':2':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.LEVEL && item.area === '香槟'
        })
      case ':3':
        return _.filter(state.devices, (item) => {
          return item.type === DEVICE_TYPES.LEVEL && item.area === '香槟'
        })
    }
  }
}

// mutations
const mutations = {
  [types.UPDATE_WATER_DEVICE_STATUS] (state, deviceStatus) {
    // 香槟项目水项目的设备有重复
    if (deviceStatus.id === '1167924262' || deviceStatus.id === '1167924743') {
      state.devices.push(deviceStatus)
    } else {
      if (!deviceStatus.id) {
        return
      }

      let finded = _.find(state.devices, (item) => {
        return item.id === deviceStatus.id
      })
      if (finded) {
        state.devices = _.filter(state.devices, (item) => {
          return item.id !== deviceStatus.id
        })
      }
      state.devices.push(deviceStatus)
    }
  },
  [types.UPDATE_WATER_STATISTICS] (state, obj) {
    if (!obj.id) {
      return
    }
    let finded = _.find(state.devices, (item) => {
      return item.id === obj.id
    })
    if (finded) {
      state.devices = _.filter(state.devices, (item) => {
        return item.id !== obj.id
      })
    }
    // 将返回的数据先存起来，等到所有都接收完成再丢进去getters中
    if (waterNumber >= 20) {
      state.devices = _.concat(state.devices, waterArr)
      waterNumber = 0
      waterArr = []
    } else {
      waterArr.push(obj)
    }
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
